package com.qf.hospital.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.system.dto.SysRoleDTO;
import com.qf.hospital.system.entity.SysRole;
import com.qf.hospital.system.entity.SysUser;
import com.qf.hospital.system.mapper.SysUserMapper;
import com.qf.hospital.system.service.SysUserService;
import com.qf.hospital.system.vo.SideTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public SysUser findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public List<SideTreeVO> findMenuByUserId(Integer userId) {
        //1. 调用mapper查询全部数据
        List<SideTreeVO> list = userMapper.findMenuByUserId(userId);

        //2. 声名一个List<SideTreeVO> result
        List<SideTreeVO> result = new ArrayList<>();

        //3. 遍历mapper查询的全部数据，将一级菜单存放到result
        for (SideTreeVO sideTreeVO : list) {
            if(sideTreeVO.getParentId() == 1){
                result.add(sideTreeVO);
            }
        }

        //4. 再次遍历mapper查询的全部数据，查询每个二级菜单，基于parentId，将当前数据存放到result的children中
        for (SideTreeVO sideTreeVO : list) {
            // 当前菜单是二级菜单
            if(sideTreeVO.getParentId() != 1 && sideTreeVO.getParentId() != 0){
                // 找到对应的一级
                for (SideTreeVO treeVO : result) {
                    // 判断当前一级菜单的id是否等于二级菜单的parentId
                    if(treeVO.getId() == sideTreeVO.getParentId()){
                        // 将当前二级菜单,添加到一级菜单的children中
                        treeVO.getChildren().add(sideTreeVO);
                        break;
                    }
                }
            }
        }

        //5. 返回数据
        return result;
    }


    @Override
    public PageInfo<SysUser> findSysUserByConditionAndPage(SysUser sysUser, Integer page, Integer limit) {
        //1. 分页
        PageHelper.startPage(page,limit);
        //2. 查询
        List<SysUser> list = userMapper.findSysUserByCondition(sysUser);
        //3. 封装
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        //4. 返回
        return pageInfo;
    }

    @Override
    public List<SysRoleDTO> findAllSysRole(Integer userid) {
        //1. 查询全部角色
        List<SysRole> roles = userMapper.findAllSysRole();

        //2. 查询当前用户拥有的角色
        List<SysRole> userRoles = userMapper.findUserRoleByUserId(userid);


        //3. 将roles中,我当前用户拥有的角色添加属性 LAY_CHECKED = true
        List<SysRoleDTO> result = new ArrayList<>();
        for (SysRole role : roles) {
            //3.1 将role信息存放到result集合的DTO对象中
            SysRoleDTO dto = new SysRoleDTO(role);
            //3.2 判断当前角色是否是用户拥有的角色
            for (SysRole userRole : userRoles) {
                if(dto.getRoleId() == userRole.getRoleId()){
                    dto.setLAY_CHECKED(true);
                    break;
                }
            }
            //3.3 存入result
            result.add(dto);
        }
        //4. 返回即可
        return result;
    }

    @Override
    @Transactional
    public void assignRoles(Integer userId, Integer[] ids) {
        //1. 删除当前用户的全部角色
        userMapper.deleteUserRolesByUserId(userId);

        if(ids != null && ids.length > 0) {
            //2. 添加当前用户的角色
            userMapper.insertUserRoles(userId, ids);
        }
    }
}
